package com.herench.bs.handler;


import com.herench.bs.agent.IPersonAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author wenkai
 */

@Component
@Handler(code = "100301",name = "密码验证")
public class LoginHandler extends AbstractMessageHandler {


    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = null;
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        String userType = requestMessage.getArgsPropertyStringValue("userType");
        String username = requestMessage.getArgsPropertyStringValue("user");
        Assert.hasLength(username,"user 不能为空！");
        String password = requestMessage.getArgsPropertyStringValue("password");
        Assert.hasLength(password,"password 不能为空！");
//        String vcode = requestMessage.getArgsPropertyStringValue("vcode");
        String vcode = null;
        String source = requestMessage.getArgsPropertyStringValue("source");
        Assert.hasLength(source,"source 不能为空！");
        String phoneInfo = requestMessage.getArgsPropertyStringValue("phoneInfo");
        String imei = requestMessage.getArgsPropertyStringValue("imei");
        String openId = requestMessage.getArgsPropertyStringValue("openId");
        //"3"微网站
        if ("3".equals(source)) {
            Assert.hasLength(openId, "微网站来源 openId 不能为空！");
        } else {
            Assert.hasLength(imei, "APP来源 imei 不能为空！");
        }
        IPersonAgent personAgent = getCleverClinicContext().getApplicationContext().getBean(IPersonAgent.class);
        Map<String, Object> res = null;
        if (StringUtils.hasLength(vcode)) {
            res = personAgent.loginByPhoneAndVerificationCode(channelCode, username, vcode);
        } else {
            res = personAgent.loginByPhoneAndPasswd(channelCode, username, password, imei, openId,source);
        }
        responseMessage = new ResponseMessage(requestMessage.getId());
        responseMessage.setData(res);
        getPropertiesFromInvokerResultMap(responseMessage, res);
        return responseMessage;
    }
}
